package com.hnote.hnoteapplication.base.utils.rsa_tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SignUtils {

    public final static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJaKH2YGVRHzCiel3Vj1OZMJLGBVAPFfFyFkvr2BbCCrmkgs2NLoLZtKcIpUI8mB3vPY6Rn1llsszFdC2WLZSKrqhhg3k4yHT+o79MhuY8OvM2HGSOqbz3p8x0DbBbQ5k/7qgrugC3iqN8knm9GW1mYEkj4fN553laSVZh2GjcQ9AgMBAAECgYAy6Tt9MZ8LlvpUNlAJicWS6INNgPH0r4gPDxdC18ZNtjtTNXeOvF7QKfitj48a0wUnyq/qy5ex9hCVYlJKj9l3D1czBsFg4xa2DiNS7C4Q744g30ZTrVUHQVLZ8RMNYpWOPpGN8fOYZtpmrq+FAQAi7KpKY8Zlo/JofDdnlom5AQJBAMZeFxQIi+WTiBTe7fx5RD1jXZk0BQhEdzJwmFqJjmNIkcSQWaqtupj17eQ4ONTcWyryLT9BTCBbc83RoBw693ECQQDCRr87yznelljDmTygwKB1qHaZkijw/+UMOFAHGnNMHpoN4N3URLhy5NYHq/9RavVnT1FBWEw3EiEWTtIr+6uNAkEAhT1l2vrynL2/xeUHjXIWt/UqRWF/j9INDkA61XVXwrBX2OPslowireNiasyov7FTO0F3yukdLQ7Adhq7pLqmwQJAcIEHvViy6nOmc58JvQ3P3lLwW36Itwd5XNo/fd2TZ0FRdTgDPimNpvtd9pNkB4IpGuqT44N+n2WgQ4rhqxbTAQJAEPxsKOKCUW7IDrVb3PdL+OmOmJKTRc6uBlfpKptcQCoqu7y6W3Dtucl1ZJ/HCIPuAO1JvK3RaU5DjH7AYfhYLA==";
    /**
     * 除去数组中的空值和签名参数
     *
     * @param sArray 签名参数组
     * @return 去掉空值与签名参数后的新签名参数组
     */
    public static Map<String, Object> paraFilter(Map<String, String> sArray) {

        Map<String, Object> result = new HashMap<String, Object>();

        if (sArray == null || sArray.size() <= 0) {
            return result;
        }

        for (String key : sArray.keySet()) {
            Object value = sArray.get(key);
            if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
                    || key.equalsIgnoreCase("sign_type")) {
                continue;
            }
            result.put(key, value);
        }

        return result;
    }

    /**
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     *
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, Object> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            Object value = params.get(key);
            if (null == value) continue;
            if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;
    }

    /**
     * rsa签名
     *
     * @param sArray
     * @param privateKey
     * @return
     */
    public static String rsaSign(Map<String, String> sArray, String privateKey) {
        String content = createLinkString(paraFilter(sArray));
        return RSA.sign(content, privateKey, "utf-8");
    }

    public static boolean rsaVerify(Map<String, String> sArray, String public_key){
        Object sign = sArray.get("sign");
        if(null==sign) return false;
        String content = createLinkString(paraFilter(sArray));
        return RSA.verify(content, sign.toString(), public_key, "utf-8");
    }

}
